/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC.core;

import core.*;
import core.comparators.BookComparator.BOOKCOMPARATORTYPE;
import core.comparators.ComparatorFactory;
import core.comparators.CourseComparator.COURSECOMPARATORTYPE;
import db.JDBC.*;
import db.core.interfaces.BookDAOCore;
import db.interfaces.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
/**
 *
 * @author Anton
 */
public class BookJdbcDAOCore implements BookDAOCore 
{
    private static BookJdbcDAOCore instance = null;
    private BookDAO bookDAO;
    private CRUDJdbcDAOCore crud;


    /**
     * 
     */
    private BookJdbcDAOCore()
    {
        JdbcDaoFactory daoFactory = JdbcDaoFactory.getInstance();
        bookDAO = daoFactory.getBookJdbcDao();
        crud = CRUDJdbcDAOCore.getInstance();
    }

    /**
     * 
     * @return
     */
    public static BookJdbcDAOCore getInstance()
    {
        if(instance == null)
            instance = new BookJdbcDAOCore();
        return instance;
    }

    /**
     * 
     * @param id
     * @return
     */
    public Book getBookById(long id)
    {
        return crud.getBook(id);
    }
    /**
     * 
     * @param ISBN10
     * @return
     */
    public Book getBookByISBN10(String ISBN10)
    {
        if(ISBN10 == null)
            return null;
        long id = bookDAO.getBookByISBN10(ISBN10).getId();
        return crud.getBook(id);
    }
    /**
     * 
     * @param ISBN13
     * @return
     */
    public Book getBookByISBN13(String ISBN13)
    {
        if(ISBN13 == null)
            return null;
        long id = bookDAO.getBookByISBN13(ISBN13).getId();
        return crud.getBook(id);
    }

    /**
     *
     * @param beginsWith
     * @param maxResult
     * @return
     */
    public Collection<String> searchForStringBooks(String beginsWith, int maxResult)
    {
        Collection<Book> titles  = bookDAO.searchForBooksByTitle(beginsWith);
        Collection<Book> authors  = bookDAO.searchForBooksByAuthor(beginsWith);
        Collection<Book> isbn10s  = bookDAO.searchForBooksByISBN10(beginsWith);
        Collection<Book> isbn13s  = bookDAO.searchForBooksByISBN13(beginsWith);

        Collection<String> t = new TreeSet<String>();
        List<String> returnCollection = new ArrayList<String>();
        if(titles != null)
            for(Book b : titles)
                if(b != null)
                    t.add(b.getTitle());
        if(authors != null)
            for(Book b : authors)
                if(b != null)
                    t.add(b.getAuthor());
        if(isbn10s != null)
            for(Book b : isbn10s)
                if(b != null)
                    t.add(b.getIsbn10());
        if(isbn13s != null)
            for(Book b : isbn13s)
                if(b != null)
                    t.add(b.getIsbn13());

        for(String s : t)
            if(s != null)
                returnCollection.add(s);

        if(returnCollection.size() < maxResult)
            maxResult = returnCollection.size();
        return returnCollection.subList(0, maxResult);
    }
    /**
     *
     * @param beginsWith
     * @return
     */
    public Collection<Book> searchForObjectBooks(String beginsWith)
    {
        Collection<Book> titles  = bookDAO.searchForBooksByTitle(beginsWith);
        Collection<Book> authors  = bookDAO.searchForBooksByAuthor(beginsWith);
        Collection<Book> isbn10s  = bookDAO.searchForBooksByISBN10(beginsWith);
        Collection<Book> isbn13s  = bookDAO.searchForBooksByISBN13(beginsWith);

        Collection<Book> t = new TreeSet<Book>(ComparatorFactory.getBookComparator(BOOKCOMPARATORTYPE.ID, true));
        List<Book> returnCollection = new ArrayList<Book>();
        if(titles != null)
            for(Book b : titles)
                if(b != null)
                    t.add(b);

        if(authors != null)
            for(Book b : authors)
                if(b != null)
                    t.add(b);
        if(isbn10s != null)
            for(Book b : isbn10s)
                if(b != null)
                    t.add(b);
        if(isbn13s != null)
            for(Book b : isbn13s)
                if(b != null)
                    t.add(b);

        for(Book b : t)
            if(b != null)
                returnCollection.add(b);
        Collections.sort(returnCollection, ComparatorFactory.getBookComparator(BOOKCOMPARATORTYPE.TITLE, true));

        return returnCollection;
    }
    /**
     * 
     * @param author
     * @return
     */
    public Collection<Book> getBooksByAuthor(String author)
    {
        if(author == null)
            return null;
        Collection<Book> books = new ArrayList<Book>();
        for(Book b : bookDAO.getBooksByAuthor(author))
            if(b != null)
                books.add(crud.getBook(b.getId()));
        return books;
    }
    /**
     * 
     * @param title
     * @return
     */
    public Collection<Book> getBooksByTitle(String title)
    {
        if(title == null)
            return null;
        Collection<Book> books = new ArrayList<Book>();
        for(Book b : bookDAO.getBooksByAuthor(title))
            if(b != null)
                books.add(crud.getBook(b.getId()));
        return books;
    }
    /**
     * 
     * @return
     */
    public Collection<Book> getAllBooks()
    {
        Collection<Book> books = new ArrayList<Book>();
        for(Book b : bookDAO.getAllBooks())
            if(b != null)
                books.add(crud.getBook(b.getId()));
        return books;
    }

    /**
     * 
     * @param book
     */
    public void addBook(Book book)
    {
        if(book == null)
            return;
        crud.addBook(book);
    }
    /**
     * 
     * @param book
     */
    public void updateBook(Book book)
    {
        if(book == null)
            return;
        crud.updateBook(book);
    }
    /**
     * 
     * @param book
     */
    public void deleteBook(Book book)
    {
        if(book == null)
            return;
        crud.deleteBook(book);        
    }
}
